package com.mimi.handler;

import com.mimi.VO.ResultVO;
import com.mimi.config.ProjectUrlConfig;
import com.mimi.exception.ResponseBankException;
import com.mimi.exception.SellException;
import com.mimi.exception.SellerAuthorizeException;
import com.mimi.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author:徐向
 * @Description:捕获SellerAuthorizeException
 * @Date: Created in 17:08 2017/10/23
 */
@ControllerAdvice
public class SellExceptionHandler {

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    //拦截登录异常
    //拼接的样子http://mimi.mynatapp.cc/wechat/qrAuthorize?returnUrl=http://mimi.mynatapp.cc/seller/login
    @ExceptionHandler(value = SellerAuthorizeException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ModelAndView handlerAuthorizeExcpetion() {
        return new ModelAndView("redirect:".concat(projectUrlConfig.getWechatOpenAuthorize()).concat("/wechat/qrAuthorize").concat("?returnUrl=").concat(projectUrlConfig.getSell().concat("/seller/login")));
    }

    @ExceptionHandler(value = SellException.class)
    @ResponseBody
    public ResultVO handlerSellerException(SellException e) {
        return ResultVOUtil.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = ResponseBankException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlerResponseBankException() {

    }
}
